package com.zhidisoft.system.action;

import java.util.Date;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.zhidisoft.base.action.BaseAction;
import com.zhidisoft.common.Page;
import com.zhidisoft.common.ResultData;
import com.zhidisoft.system.entity.User;
import com.zhidisoft.system.vo.UserVO;

@SuppressWarnings("all")
@Controller
@Scope("prototype")
public class UserAction extends BaseAction{
	
	private User user;
	
	private UserVO userVO;
	
	private String ids;
	
	/**
	 * 直接转发至list_user.jsp
	 * @return
	 */
	public String list(){
		return "list";
	}
	
	/**
	 * 直接转发到assign_user.jsp
	 * @return
	 */
	public String assign(){
		return "assign";
	}
	
	/**
	 * 直接转发至edit_user.jsp
	 * @return
	 */
	public String edit(){
		if(user!=null && StringUtils.isNotBlank(user.getId())){
			userVO = userService.findById(user.getId());
		}
		return "edit";
	}
	
	/**
	 * 更新用户角色
	 * @return
	 */
	public String updateRoles() throws Exception{
		String[] roleIds = ids.split(",");
		boolean b = userService.updateRoles(user.getId(), roleIds);
		if(b){
			resultData = ResultData.buildSuccessResult("为用户分配角色成功");
			
		}else{
			resultData = ResultData.buildFailureResult("为用户分配角色失败");
		}
		printJSONObject(resultData);
		return NONE;
	}
	
	/**
	 * 更新或者保存用户信息
	 * @return
	 * @throws Exception
	 */
	public String saveOrUpdate() throws Exception{
		//不管保存还是更新，将表单中的值设置给userVO
		user = UserVO.toUser(userVO);
		//新添加的用户是由当前登录用户创建
		if(StringUtils.isBlank(user.getId())){
			user.setId(null);
			user.setCreateUser(this.getCurrentUser());
			user.setCreateTime(new Date());
		}else{
			user.setUpdateTime(new Date());
			user.setUpdateUser(this.getCurrentUser());
		}
		boolean b = userService.saveOrUpdate(user);
		if(b){
			resultData = ResultData.buildSuccessResult("操作用户信息成功！");
		}else{
			resultData = ResultData.buildFailureResult("操作用户信息失败!");
		}
		printJSONObject(resultData);
		return NONE;
	}
	
	/**
	 * 处理登录请求的action
	 * @return
	 * @throws Exception
	 */
	public String login() throws Exception{
		//判断用户名是否为空
		if(StringUtils.isBlank(user.getUsername())){
			resultData = ResultData.buildFailureResult("用户名不能为空");
			printJSONObject(resultData);
			return NONE;
		}
		//判断密码是否为空
		if(StringUtils.isBlank(user.getPassword())){
			resultData = ResultData.buildFailureResult("用户密码不能为空");
			printJSONObject(resultData);
			return NONE;
		}
		//查询用户信息
		User user1 = userService.findByUsername(user.getUsername());
		//判断是否存在指定用户
		if(user1 == null){
			resultData = ResultData.buildFailureResult("用户名不存在");
			printJSONObject(resultData);
			return NONE;
		}
		//判断密码是否正确
		if(!user1.getPassword().equals(user.getPassword())){
			resultData = ResultData.buildFailureResult("用户密码错误");
			printJSONObject(resultData);
			return NONE;
		}
		
		//判断用户状态是否为可用
		if(user1.getStatus()!=1){
			resultData = ResultData.buildFailureResult("用户被禁止登陆，请与系统管理员联系！");
			printJSONObject(resultData);
			return NONE;
		}
		
		//将用户信息保存到Session 中
		HttpSession session = ServletActionContext.getRequest().getSession();
		session.setAttribute("user", user1);
		//输出成功信息
		resultData = ResultData.buildSuccessResult("登录成功");
		printJSONObject(resultData);
		
		return NONE;
	}
	
	/**
	 * 分页形式获取用户列表信息
	 * @return
	 */
	public String findByPage() throws Exception{
		
		Page pageBean = userService.findByPage(page,rows);
		resultData = ResultData.buildSuccessResult("获取用户列表成功", pageBean);
		printJSONObject(resultData);
		
		return NONE;
	}
	
	public String remove() throws Exception{
		boolean b = userService.removeByIds(ids);
		if(b){
			resultData = ResultData.buildSuccessResult("删除用户信息成功!");
		}else{
			resultData = ResultData.buildFailureResult("删除用户信息失败!");
		}
		printJSONObject(resultData);
		
		return NONE;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public UserVO getUserVO() {
		return userVO;
	}

	public void setUserVO(UserVO userVO) {
		this.userVO = userVO;
	}
	
}
